import type * as pojo from 'shared-types'
import { dialog } from 'electron'
import { useRepository } from './repository'
import { defineBean } from '../util'

export function createController() {
  const repository = useRepository()

  async function login(query: pojo.LoginQuery): Promise<pojo.LoginCtrlVo> {
    const userInfo = await repository.login(query)
    const platforms = await repository.listPlatforms()
    const pages = await repository.listPages()
    return {
      userInfo,
      platforms: platforms.list,
      pages: pages.list
    }
  }
  function onNotice(query: pojo.OnNoticeQuery) {
    repository.onNotice(query)
  }
  function listNotices() {
    return repository.listNotices()
  }
  async function readNotice(query: pojo.Notice) {
    await dialog.showMessageBox({
      type: 'info',
      title: '通知',
      message: query.content
    })
    return repository.readNotice({ id: query.id })
  }
  function listCookies(query: pojo.ListCookiesQuery) {
    return repository.listCookies(query)
  }
  function updateCookies(query: pojo.UpdateCookiesQuery) {
    return repository.updateCookies(query)
  }

  return {
    login,
    onNotice,
    listNotices,
    readNotice,
    listCookies,
    updateCookies
  }
}

export const useController = defineBean(createController)
